﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using MacomberMapSystem.Common.Serialization;
using MacomberMapSystem.Common.Database;
using MacomberMapSystem.Common.CIM;
using MacomberMapSystem.Common.Integration;
using MacomberMapSystem.Common.User_Interfaces;
using System.Windows.Forms;

namespace MacomberMapSystem.Common.Processing
{
    /// <summary>
    /// This class handles coordinate validation, pausing progress until the coordinate issues are repaired
    /// </summary>
    public class CIM_Coordinate_Validator : CIM_Processor
    {
        /// <summary>
        /// Initialize the coordinate validation
        /// </summary>
        /// <param name="xElem"></param>
        /// <param name="NamedParameters"></param>
        /// <param name="Repository"></param>
        public CIM_Coordinate_Validator(XmlElement xElem, Dictionary<String, Object> NamedParameters, MM_Repository Repository)
            : base(xElem, NamedParameters, Repository)
        {
        }

        /// <summary>
        /// Initiate our coordinate validation process
        /// </summary>
        /// <param name="DbConn"></param>
        /// <returns></returns>
        public override enumProcessState InitiateProcess(MM_Database_Connector DbConn)
        {
             //First, build our list of elements to be processed
            MM_Database_Model Model = NamedParameters["Model"] as MM_Database_Model;
            List<CIM_Element> Substations = new List<CIM_Element>();
            Dictionary<CIM_Element, String> SubstationsNeedingValidation = new Dictionary<CIM_Element, string>();
            foreach (CIM_Element SubElem in Model.ElementsOfType("cim:Substation"))
                if (bool.Parse(SubElem["etx:Substation.forNetwork"]) && bool.Parse(SubElem["etx:Substation.forScada"]))
                    Substations.Add(SubElem);
                        Total = Substations.Count;
            Current = 0;

            MM_Substation FoundSub;
            foreach (CIM_Element Sub in Substations)
            {
                if (!Repository.Db.Substations.TryGetValue(Sub.rdfID, out FoundSub))
                {
                    SubstationsNeedingValidation.Add(Sub, Sub.Name + " has no coordinates.");
                    ProcessLog.AppendLine("Substation " + Sub.Name + " (" + Sub.TEID + ") has no coordinates.");
                }
                else if (FoundSub.LatLong.IsNan)
                {
                    SubstationsNeedingValidation.Add(Sub, Sub.Name + " has unknown coordinates.");
                    ProcessLog.AppendLine("Substation " + Sub.Name + " (" + Sub.TEID + ") has unknown coordinates.");
                }
                else if ((FoundSub.Longitude < Repository.Db.StateBoundary.Min.X) || (FoundSub.Longitude > Repository.Db.StateBoundary.Max.X) || (FoundSub.Latitude < Repository.Db.StateBoundary.Min.Y) || (FoundSub.Latitude > Repository.Db.StateBoundary.Max.Y))
                {
                    SubstationsNeedingValidation.Add(Sub, Sub.Name + " is outside the state boundary.");
                    ProcessLog.AppendLine("Substation " + Sub.Name + " (" + Sub.TEID + ") is outside the state boundary.");
                }
                Current++;
            }           
            return enumProcessState.Completed;
        }

        /// <summary>
        /// Report the name of our process
        /// </summary>
        public override string ProcessName
        {
            get { return "Coordinate validation"; }
        }
    }
}
